package services;

import dao.ErrorLog_Repository;
import models.*;
import play.db.jpa.JPAApi;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import java.util.stream.Stream;

import static java.util.concurrent.CompletableFuture.supplyAsync;


/**
 * Provide JPA operations running inside of a thread pool sized to the connection pool
 */
public class ErrorLog_Repository_JPA implements ErrorLog_Repository {

    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public ErrorLog_Repository_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }
    
    @Override
    public CompletionStage<List<ErrorLog_T_Entity>> getErrorLog ()
    {
    	return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
            	return entityManager.createQuery("select e from ErrorLog_T_Entity e ", ErrorLog_T_Entity.class).getResultList();
            });
        }, executionContext);    	
    }
}